package study.thread.atomic;

/*
 * 测试类：多线程对原子计数器进行操作
 */
public class AtomicCounterTest extends Thread {
	
	private AtomicCounter atomicCounter;
	
	public AtomicCounterTest(AtomicCounter atomicCounter) {
		this.atomicCounter = atomicCounter;
	}
	
	public void run() {
		int i = atomicCounter.increment();
		System.out.println(Thread.currentThread().getName()+" AtomicCounterTest :: increment()==> "+i+" atomicCounter.getValue()==> "+atomicCounter.getValue());
	}
	
	public static void main(String[] args) throws InterruptedException {
		
		Long start = System.currentTimeMillis();
		
		AtomicCounter atomicCounter = new AtomicCounter();
		
		for (int i = 0; i < 10; i++) {
			Thread t = new AtomicCounterTest(atomicCounter);
			//t.sleep(10);
			t.start();
			//t.join();
		}
		
		for (int i = 0; i < 10; i++) {
			Thread thread = new TestAtomic(atomicCounter);
			thread.start();
			//thread.join();
		}

		Long end = System.currentTimeMillis();
		
		//System.out.println((end-start)/(1000*60)+" minutes");
	}
}

//测试原子类
class TestAtomic extends Thread {
	private AtomicCounter atomicCounter;
	
	public TestAtomic(AtomicCounter atomicCounter) {
		this.atomicCounter = atomicCounter;
	}
	
	public void run() {
		int i = atomicCounter.increment();
		System.out.println(Thread.currentThread().getName()+" TestAtomic :: increment()==> "+i+" atomicCounter.getValue()==> "+atomicCounter.getValue());
	}
	
}
